package restful_apis.query_understanding;

import javax.annotation.PostConstruct;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import javax.inject.Inject;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.orientechnologies.orient.server.handler.OJMXPlugin;
import common.helper.json.JavaToJsonString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import query_understanding.common.nlp_toolkit.DependencyParser;
import query_understanding.query_understand.tree_generator.QueryTreeGenerator;
import query_understanding.query_understand.tree_progress.DocumentQueryProgress;
import query_understanding.query_understand.tree_selector.QueryTreeSelector;
import restful_apis.common.MediaTypes;
import restful_apis.dependency.BeanExample;

import javax.ws.rs.core.Application;

/**
 * Created by julianzliu on 4/25/2017.
 */


@Path("/queryparser")
@Produces(MediaTypes.JSON_UTF8)
//@Produces(MediaType.APPLICATION_JSON)
@Component
public class QueryTreeGeneratorRest {
    static  final Logger logger = LoggerFactory.getLogger(QueryTreeGeneratorRest.class);

    private QueryTreeGenerator generator;
    private QueryTreeSelector selector;
    private ObjectMapper mapper;
    private DependencyParser.RelationsExtractionTester tester;

    //@Inject
    public QueryTreeGeneratorRest( ){

        this.generator = new QueryTreeGenerator();

        this.selector = new QueryTreeSelector();

        this.mapper = new ObjectMapper();

        this.tester = new DependencyParser.RelationsExtractionTester();

    }

    //@PostConstruct
    private void init() {
        logger.info("Initialized /queryparser Successfully !!!");
    }


    @GET
    @Path("/_parsing")
    @Produces(MediaTypes.JSON_UTF8)
    //@Produces(MediaType.APPLICATION_JSON)
    public String  parseQuery(
            @QueryParam("query") String query
    ){
        DocumentQueryProgress result =  this.generator.parseQuery( query );
        ObjectNode json = (ObjectNode)( result.genAllSentenceCanditateTrees() );

        return  JavaToJsonString.mapJavaToRestJson( json );

    }


    @GET
    @Path("/_generate")
    @Produces(MediaTypes.JSON_UTF8)
    //@Produces(MediaType.APPLICATION_JSON)
    public String  genarateQuery(
            @QueryParam("query") String query
    ){
        DocumentQueryProgress result =  this.selector.genDocQueryTree( query );  //.parseQuery( query );
        ObjectNode json = (ObjectNode)( result.genDocumentTreeJson() );

        logger.info("[genarateQuery] query finished!");

        return  JavaToJsonString.mapJavaToRestJson( json );

    }


    @GET
    @Path("/_relation")
    @Produces(MediaTypes.JSON_UTF8)
    //@Produces(MediaType.APPLICATION_JSON)
    public String  extractRelation(
            @QueryParam("query") String query,
            @QueryParam("start") String start
    ){

        JsonNode ret = this.tester.testExtractRelations(query, start);

        return JavaToJsonString.mapJavaToJson( ret );

    }




}
